package com.jymj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jymj.entity.ComplaintsEntity;
import com.jymj.entity.ComplaintsFile;
import com.jymj.entity.dto.ComplaintsDto;
import com.jymj.entity.vo.ComplaintsVo;
import com.jymj.entity.zjd.ApplyFileEntity;
import com.jymj.service.ComplaintsService;
import com.jymj.utils.FileUtile.FileUploadEntity;
import com.jymj.utils.FileUtile.FileUtile;
import com.jymj.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Created by ${lc} on 2021/6/21. 接诉即办
 */
@RestController
@RequestMapping("/complaints")
public class ComplaintsController {
    @Autowired
    private ComplaintsService complaintsService;

    private final String APPLE_SAVE = "ComplaintFile.dirPath";

    /**
     * 列表 可分页
     * @param complaintsDto
     * @return
     */
    @PostMapping("/queryList")
    public R queryList(@RequestBody ComplaintsDto complaintsDto){
        PageHelper.startPage(complaintsDto.getPage(),complaintsDto.getLimit());
        List<ComplaintsEntity> complaintsEntities = complaintsService.queryList(complaintsDto);
        PageInfo<ComplaintsEntity> complaintsEntityPageInfo = new PageInfo<>(complaintsEntities);
        return R.ok().put("data",complaintsEntityPageInfo);
    }

    @PostMapping("/save")
    public R save(@RequestBody ComplaintsEntity complaintsEntity){
        complaintsService.save(complaintsEntity);
        return R.ok("修改成功");
    }

    @PostMapping("/update")
    public R update(@RequestBody ComplaintsEntity complaintsEntity){
        complaintsService.update(complaintsEntity);
        return R.ok("修改成功");
    }

    @PostMapping("/delete")
    public R delete(@RequestBody Long... ids){
        //删除图片信息
        List<ComplaintsFile> complaintsFiles = complaintsService.queryFiles(ids);
        for (ComplaintsFile complaintsFile : complaintsFiles) {
            complaintsService.deleteFile(complaintsFile.getId());
            FileUploadEntity fileUploadEntity = new FileUtile().initFileUtile(APPLE_SAVE);
            FileUtile.fileDelete(complaintsFile.getPath(),fileUploadEntity);
        }
        complaintsService.delete(ids);
        return R.ok("删除成功");
    }

    /**
     *按乡镇 统计表
     * @param complaintsDto
     * @return
     */
    @PostMapping("/statistical")
    public R statistical(@RequestBody ComplaintsDto complaintsDto){
        List<ComplaintsVo> complaintsVos = complaintsService.statistical(complaintsDto);
        return R.ok().put("data",complaintsVos);
    }

    /**
     * 图片上传
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    public R uploadFile(MultipartFile file){
        FileUtile fileUtile = new FileUtile();
        FileUploadEntity fileUploadEntity = fileUtile.initFileUtile(APPLE_SAVE);
        fileUploadEntity= FileUtile.fileUpload(file, fileUploadEntity);
        ComplaintsFile complaintsFile = complaintsService.saveFile(fileUploadEntity);
        return R.ok().put("data",complaintsFile);
    }

    /**
     * 删除图片
     * @param ids
     * @return
     */
    @PostMapping("/deleteFile")
    public R deleteFile(@RequestBody Integer... ids){
        for(int i=0;i<ids.length;i++){
            ComplaintsFile complaintsFile = complaintsService.queryObject(ids[i]);
            if(complaintsFile != null){
                complaintsService.deleteFile(ids[i]);
                FileUploadEntity fileUploadEntity = new FileUtile().initFileUtile(APPLE_SAVE);
                FileUtile.fileDelete(complaintsFile.getPath(),fileUploadEntity);
            }
        }
        return R.ok("删除成功！");
    }
}
